﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace _06.原型模式.Helper
{
    public class SerializableHelper
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public string Serializable(object target)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                new BinaryFormatter().Serialize(stream, target);
                return Convert.ToBase64String(stream.ToArray());
            }
        }

        public object Derializable(string target)
        {
            byte[] array = Convert.FromBase64String(target);
            using (MemoryStream stream = new MemoryStream(array))
            {
                return new BinaryFormatter().Deserialize(stream);
            }
        }

        public T Derializable<T>(string target)
        {
            return (T)Derializable(target);
        }
    }
}
